package com.xnx3.wangmarket.plugin.translate.controller;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.StringUtil;
import com.xnx3.j2ee.pluginManage.controller.BasePluginController;
import com.xnx3.j2ee.service.SqlCacheService;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.AttachmentUtil;
import com.xnx3.j2ee.util.CacheUtil;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.admin.entity.Site;
import com.xnx3.wangmarket.admin.util.ActionLogUtil;
import com.xnx3.wangmarket.admin.util.SessionUtil;
import com.xnx3.wangmarket.plugin.translate.entity.Translate;
import com.xnx3.wangmarket.plugin.translate.vo.TranslateVO;

/**
 * 功能插件开发入门示例
 * @author 管雷鸣
 */
@Controller(value="translateAdminIndexPluginController")
@RequestMapping("/plugin/translate/admin/")
public class IndexController extends BasePluginController {
	@Resource
	private SqlService sqlService;
	@Resource
	private SqlCacheService sqlCacheService;
	
	@RequestMapping(value="detail.json", method = RequestMethod.POST)
	@ResponseBody
	public TranslateVO detail(HttpServletRequest request) {
		TranslateVO vo = new TranslateVO();
		if(!haveSiteAuth()) {
			vo.setBaseVO(BaseVO.FAILURE, "清先登录");
			return vo;
		}
		vo.setTranslate(getTranslate());
		return vo;
	}
	
	/**
	 * 设置保存
	 * @param enable 是否启用，是否在当前网站开启翻译能力。 1启用，0禁用
	 * @param javascript js脚本内容。也就是 translate.execute() 之前自定义的东西
	 * @param css
	 */
	@RequestMapping(value="save.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO save(HttpServletRequest request,
			@RequestParam(value = "enable", required = false , defaultValue="") Short enable,
			@RequestParam(value = "javascript", required = false , defaultValue="") String javascript,
			@RequestParam(value = "css", required = false , defaultValue="") String css
			){
		if(!haveSiteAuth()) {
			return error("清先登录");
		}
		
		BaseVO vo = new BaseVO();
		
		Site cacheSite = SessionUtil.getSite();
		if(cacheSite == null) {
			return error("网站不存在");
		}
		//Site site = sqlService.findById(Site.class, cacheSite.getId());
		
		Translate translate = getTranslate();
		translate.setEnable(enable);
		translate.setJavascript(javascript);
		translate.setCss(css);
		sqlService.save(translate);
		
		//清楚缓存-刷新目的
		sqlCacheService.deleteCacheById(Translate.class, translate.getId());
		
		ActionLogUtil.insertUpdateDatabase(request, "修改 translate 设置保存");
		
		return vo;
	}
	
	/**
	 * 获取当前登录网站的 translate 
	 * @return
	 */
	private Translate getTranslate() {
		Site cacheSite = SessionUtil.getSite();
		Translate translate = sqlService.findById(Translate.class, cacheSite.getId());
		if(translate == null) {
			translate = new Translate();
			translate.setId(cacheSite.getId());
		}
		return translate;
	}
}